package com.example.goodpet.home.homeFocus.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.activity.LoginActivity;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.IsLoginPage;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.home.homeFocus.adapter.HomeFocusFragmentAdapter;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class HomeFocusFragment extends Fragment {
    String TAG=this.getClass().getSimpleName();
    private final int LOGIN_REQ=100;

    private RecyclerView recyclerView;

    private Retrofit retrofit;
    private UserService userService;
    private HomeFocusFragmentAdapter homeFocusFragmentAdapter;
    private LoadMoreWrapper loadMoreWrapper;
    private List<UserPetInfo> data=new ArrayList<>();
    private IsLoginPage isLoginPage;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        isLoginPage=new IsLoginPage(getActivity()) {
            @Override
            public int getLayoutXMLId() {
                return R.layout.fragment_home_focus;
            }
        };

        recyclerView=isLoginPage.findViewById(R.id.recyclerView);
        retrofit=RetrofitSingleton.getRetrofitNetConfig();
        userService=retrofit.create(UserService.class);
        initListener();
        return isLoginPage;
    }

    private void initListener() {
        isLoginPage.layoutUnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent,100);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==getActivity().RESULT_OK){
            //自动执行onResume方法
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        if (MyApplication.user==null)isLoginPage.setStateCurrent(IsLoginPage.STATE_UN_LOGIN);
        else {
            initData();
        }
    }

    /**
     * 初始化主页面Fragment的数据
     */
    public void initData() {
        isLoginPage.setStateCurrent(IsLoginPage.STATE_loading);
        userService.getMyFollowUpdate(MyApplication.user.getId(),1).enqueue(new Callback<ResponseBody<List<UserPetInfo>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<UserPetInfo>>> call, Response<ResponseBody<List<UserPetInfo>>> response) {
                ResponseBody<List<UserPetInfo>> body = response.body();
                if (body!=null){
                    if (body.getCode()==200){
                        isLoginPage.setStateCurrent(IsLoginPage.STATE_SUCCESS);
                        data=body.getData();
                        //Log.d(TAG, "onResponse: "+);
                        homeFocusFragmentAdapter = new HomeFocusFragmentAdapter(data,getActivity());
                        loadMoreWrapper = new LoadMoreWrapper(homeFocusFragmentAdapter);
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                userService.getMyFollowUpdate(MyApplication.user.getId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<UserPetInfo>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<UserPetInfo>>> call, Response<ResponseBody<List<UserPetInfo>>> response) {
                                        ResponseBody<List<UserPetInfo>> body1 = response.body();
                                        //Log.d(TAG, "onResponse: "+ body);
                                        if (body1.getCode() == 200) {
                                            List<UserPetInfo> data1 = body1.getData();
                                            data.addAll(data1);
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                        } else if (body1.getCode() == 404) {
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                        }

                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<UserPetInfo>>> call, Throwable t) {
                                        UIUtils.toast("连接服务器失败", false);
                                    }

                                });
                    }
                });
                         }else if (body.getCode()==404){
                        isLoginPage.setStateCurrent(IsLoginPage.STATE_EMPTY);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<UserPetInfo>>> call, Throwable t) {
                isLoginPage.setStateCurrent(IsLoginPage.STATE_ERROR);
            }
        });

    }
}
